/*****************************************************************************
 *  CommLayerLib - CommLayerPort.java
 *  Copyright (C) 2012  Matteo Piscitelli
 *  E-mail: matteo@picciux.it
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  For further info, check http://code.google.com/p/java-comm-layer/
 *
 *  SVN: $Id: CommLayerPort.java 23 2012-10-02 16:59:05Z picciux@gmail.com $
 *  
 *****************************************************************************/

package it.picciux.commlayer;

public class CommLayerPort {
	private String name;
	private String description;
	
	public CommLayerPort(String s) {
		this.name = getName(s);
		this.description = getDescription(s);
	}
	
	public CommLayerPort(String name, String description) {
		this.name = name;
		
		if (description != null)
			this.description = description;
		else
			this.description = name;
	}


	public String getName() {
		return name;
	}


	public String getDescription() {
		return description;
	}

	public String toString() {
		return description;
	}
	
	public String getCompleteString() {
		return description + "@" + name;		
	}
	
	public boolean equals(CommLayerPort anotherPort) {
		return (name.equalsIgnoreCase(anotherPort.getName()) && description.equalsIgnoreCase(anotherPort.getDescription()));
	}
	
	public static String getName(String s) {
		int idx = s.indexOf("@");
		
		if (idx == -1) return s;
		
		return s.substring(idx + 1);		
	}
	
	public static String getDescription(String s) {
		int idx = s.indexOf("@");
		
		if (idx == -1) return s;
		
		if (idx < 1) return s.substring(idx + 1);
		
		return s.substring(0, idx);		
	}

}
